package com.example.news360_08;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;


/**
 * Created by wanghao on 2016/5/23.
 */
public class MyAdapter extends BaseAdapter{
    private  List<JavaBean.DataItem> mDatas;
    private  Context mContext;
    private JavaBean.DataItem mDataItem;

    public MyAdapter(Context context, List<JavaBean.DataItem> data) {
        this.mContext = context;
        this.mDatas = data;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //获取对应条目的类型
    @Override
    public int getItemViewType(int position) {
        mDataItem = mDatas.get(position);
        if (mDataItem.imgs.size() == 3){//类型一,3张图片
            return 0;//类型必须从0开始设置
        }else if(mDataItem.imgs.size() == 1){//类型二,1张图片
            return 1;
        }else {//类型三,没有图片
            return 2;
        }
    }

    //获取多条目展示的类型数量
    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int itemViewType = getItemViewType(position);
        ViewHolder holder = null;

        switch (itemViewType){
            case 0://类型一,3张图片
                if (convertView == null){
                    View.inflate(mContext,R.layout.list_item_new3,null);
                    holder = new ViewHolder();
                    holder.img1 = (ImageView) convertView.findViewById(R.id.img1);
                    holder.img2 = (ImageView) convertView.findViewById(R.id.img2);
                    holder.img3 = (ImageView) convertView.findViewById(R.id.img3);
                    holder.tv = (TextView) convertView.findViewById(R.id.title);
                    convertView.setTag(holder);
                }else {
                    holder = (ViewHolder) convertView.getTag();
                }
                holder.tv.setText(mDataItem.title);
                Glide.with(mContext).load(mDataItem.imgs.get(0)).into(holder.img1);
                Glide.with(mContext).load(mDataItem.imgs.get(1)).into(holder.img2);
                Glide.with(mContext).load(mDataItem.imgs.get(2)).into(holder.img3);
                return convertView;
            case 1://类型二,1张图片
                if (convertView == null) {
                    convertView = View.inflate(mContext, R.layout.list_item_new2, null);
                    holder = new ViewHolder();
                    holder.img1 = (ImageView) convertView.findViewById(R.id.img1);
                    holder.tv = (TextView) convertView.findViewById(R.id.title);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
                holder.tv.setText(mDataItem.title);
                Glide.with(mContext).load(mDataItem.imgs.get(0)).into(holder.img1);
                return convertView;
            case 2://类型三,没有图片
                if (convertView == null) {
                    convertView = View.inflate(mContext, R.layout.list_item_new1, null);
                    holder = new ViewHolder();
                    holder.tv = (TextView) convertView.findViewById(R.id.title);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
                holder.tv.setText(mDataItem.title);
                return convertView;
        }
        return convertView;
    }
    public static class ViewHolder{
        private TextView tv;
        private ImageView img1;
        private ImageView img2;
        private ImageView img3;
    }
}
